
function startButtonClick() {
    var buttons = document.getElementsByClassName('btn72');
    var gt2dz = document.getElementById('cc_seat_type_O_check');
    gt2dz?.click();
    var kyw = document.getElementById('cc_seat_type_3_check');
    kyw?.click();
    var kyz = document.getElementById('cc_seat_type_1_check');
    kyz?.click();

    var mBool = false;
    for (let index = 0; index < buttons.length; index++) {
        var button = buttons[index];
        if (button!='') {
            var mHCNumber = button.parentNode.parentNode.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0]?.innerHTML;
            if(mListAll.length == 0 || (mListAll.length > 0 && mListAll.includes(mHCNumber))){
                // console.log(mHCNumber);
                if (mAutos.includes('zdcx')) {
                    button?.click();
                    mBool = true;
                }
                break;
            }
        }
    }
    if(!mBool){
        setTimeout(function() {
            // window.location.reload();
            var inquireB = document.getElementById('query_ticket');
            if (mAutos.includes('zdcx')) {
                inquireB?.click();
            }
        }, 500);
    }
}


function handleCheckboxChange(e) {
    var mVal = e.target.value;
    if(e.target.checked){
        mListAll.push(mVal);
    }else{
        mListAll = mListAll.filter(item => item !== mVal);
    }
    localStorage.setItem('mListAll',JSON.stringify(mListAll));
}
function autoCheckboxChange(e) {
    var mVal = e.target.value;
    if(e.target.checked){
        mAutos.push(mVal);
    }else{
        mAutos = mAutos.filter(item => item !== mVal);
    }
    localStorage.setItem('mAutos',JSON.stringify(mAutos));
}

function handleUserInputChange(e) {
    if(e.target.value){
        mInputs = e.target.value.split(',');
    }
    localStorage.setItem('mInputs',JSON.stringify(mInputs));
}

function crateCheck(mValue,mName,index,mFun) {
    var checkbox = document.createElement("input");
    checkbox.type = "checkbox";
    checkbox.className = "plugin-right-c";
    checkbox.id = "checkbox" + index;
    checkbox.name = mValue;
    checkbox.value = mValue;
    if(index == 'zdcx' || index == 'zdtj' || index == 'zdqr'){
        checkbox.checked = mAutos.includes(mValue);;
    }else {
        checkbox.checked = mListAll.includes(mValue);
    }
    checkbox.addEventListener("change",mFun);
    var label = document.createElement("label");
    label.htmlFor = checkbox.id;
    label.textContent = mName;
    label.className = "plugin-right";
    label.appendChild(checkbox);
    return label;
}
function mIntLoad() {
    var mAll = localStorage.getItem('mListAll');
    var mAut = localStorage.getItem('mAutos');
    var mInp = localStorage.getItem('mInputs');
    if(mAll){
        mListAll = JSON.parse(mAll);
    }else{
        mListAll = [];
    }
    if(mAut){
        mAutos = JSON.parse(mAut);
    }else{
        mAutos = [];
    }
    if(mInp){
        mInputs = JSON.parse(mInp);
    }else{
        mInputs = [];
    }
}
function uiBody() {
    var tableList = document.getElementById('queryLeftTable');
    var rowCount = tableList.rows.length;

    var mCCDiv = document.createElement("div");
    mCCDiv.className = "plugin-one";
    for (let index = 0; index < rowCount; index++) {
        var firstCellRowSpan = tableList.rows[index].cells.length;
        if(firstCellRowSpan > 1){
            var mCZDom = tableList.rows[index].cells[0].childNodes[0];
            var mCZNumber = mCZDom.childNodes[0].childNodes[0].childNodes[0]?.innerHTML;

            mCCDiv.appendChild(crateCheck(mCZNumber,mCZNumber,index,handleCheckboxChange));
        }
    }

    var mPluDiv = document.createElement("div");
    mPluDiv.className = "plugin";
    var mAllDome = document.createDocumentFragment();

    var mH3CC = document.createElement("h3");
    mH3CC.className = "plugin-title";
    mH3CC.textContent = "选择车次";
    mAllDome.appendChild(mH3CC);
    mAllDome.appendChild(mCCDiv);

    var mH3User = document.createElement("h3");
    mH3User.className = "plugin-title";
    mH3User.textContent = "乘车人序号";
    var mCUser = document.createElement("div");
    mCUser.className = "plugin-one";
    var mCUserInput = document.createElement("input");
    mCUserInput.type = "text";
    mCUserInput.placeholder = "乘车人序号0开始多人,隔开";
    mCUserInput.value = mInputs.join(",");
    mCUserInput.addEventListener("change",handleUserInputChange);
    mCUser.appendChild(mCUserInput);
    mAllDome.appendChild(mH3User);
    mAllDome.appendChild(mCUser);

    var mH3Auto = document.createElement("h3");
    mH3Auto.className = "plugin-title";
    mH3Auto.textContent = "自动化功能";
    var mCAuto = document.createElement("div");
    mCAuto.className = "plugin-one";
    mCAuto.appendChild(crateCheck('zdcx','自动查询购买','zdcx',autoCheckboxChange));
    mCAuto.appendChild(crateCheck('zdtj','自动提交','zdtj',autoCheckboxChange));
    mCAuto.appendChild(crateCheck('zdqr','自动确认','zdqr',autoCheckboxChange));
    mAllDome.appendChild(mH3Auto);
    mAllDome.appendChild(mCAuto);

    mPluDiv.appendChild(mAllDome);
    var body = document.body;
    body.appendChild(mPluDiv);
}

window.onload = function() {

    //设置界面
    mIntLoad();
    uiBody();
    
    setTimeout(startButtonClick,500);

    chrome.runtime.sendMessage({
        name: 'load',
        content:''
    });
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    if (message.name === "list") {
      requestAnimationFrame(function () {
        setTimeout(startButtonClick,500);
      });
    }
});